
package com.yimixian.app.activity;

import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.AppService;
import com.yimixian.app.R;
import com.yimixian.app.YMXApplication;
import com.yimixian.app.common.Constants;
import com.yimixian.app.fragment.HomeFragment;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.ui.FragmentGroupActivity;
import com.yimixian.app.util.ServiceUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

public class BaseNaviFragmentGroupActivity extends FragmentGroupActivity {

    private final static  String TAG = "BaseNaviFragGroActivity";
    /**
     * 倒计时服务部分
     */
    private BroadcastReceiver mNotifyDataReceiver;
    private ServiceConnection conn;
    private List<GoodsItem> list;
    private AppService getAppService;
    private String pageType;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

    }

    /**
     * 倒计时服务提示刷新对应页面
     */
    public class NotifyDataReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Constants.ServicePageType.TOUNBINDSERVICE.equals(action)){
                unBindService();
            }else{
                onReiveServiceNotice(action);
            }
        }
    }

    /**
     * service通信传递数据
     * @param list
     * @param pageType
     */
    public void initCountDownData(List<GoodsItem> list, String pageType){
        this.list = list;
        this.pageType = pageType;
        if (getAppService != null){
            setData(getAppService);
            return;
        }
        if (mNotifyDataReceiver == null) mNotifyDataReceiver = new NotifyDataReceiver();
            conn = ServiceUtils.bindAndGetService(this, conn, mNotifyDataReceiver,
                    new ServiceUtils.GetAppServiceListener() {
                        @Override
                        public void getService(AppService appService) {
                            setData(appService);
                            getAppService = appService;
                        }
                    });
        }

    /**
     * 清除服务里对应的页面数据或者所有数据
     * @param pageType 清除对应页面数据  为空清除所有数据
     */
    public void clearServiceMapData(String pageType){
        if (getAppService != null){
            getAppService.clearMapData(pageType);
        }
    }
    public void setData(AppService appService){
        appService.setGoodsItem(list, pageType);
    }

    public void unBindService(){
        ServiceUtils.unbindService(this, conn);
        getAppService = null;
    }

    public void onReiveServiceNotice(String action){
    }


    public void setTopNavi(int resLeft, String resRight, String title,
            OnClickListener rightAction, OnClickListener leftAction) {
        ImageButton navi_left = (ImageButton) findViewById(R.id.title_bar_left_btn);
        TextView navi_right = (TextView) findViewById(R.id.title_bar_right_text);
        TextView navi_title = (TextView) findViewById(R.id.title_bar_title);
        findViewById(R.id.title_bar_right_btn).setVisibility(View.GONE);
        findViewById(R.id.title_bar_logo_title).setVisibility(View.GONE);
        if (resLeft == 0) {
            navi_left.setVisibility(View.GONE);
        } else {
            navi_left.setVisibility(View.VISIBLE);
            navi_left.setImageResource(resLeft);
        }
        if (TextUtils.isEmpty(resRight)) {
            navi_right.setVisibility(View.GONE);
        } else {
            navi_right.setVisibility(View.VISIBLE);
            navi_right.setText(resRight);
        }
        if (TextUtils.isEmpty(title)) {
            navi_title.setVisibility(View.VISIBLE);
        } else {
            navi_title.setVisibility(View.VISIBLE);
            navi_title.setText(title);
        }
        if (leftAction == null) {
            navi_left.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    onBackPressed();
                }
            });
        } else {
            navi_left.setOnClickListener(leftAction);
        }
        navi_right.setOnClickListener(rightAction);
    }

    public void setTopNavi(int resLeft, int resRight, String title,
            OnClickListener rightAction, OnClickListener leftAction) {
        ImageButton navi_left = (ImageButton) findViewById(R.id.title_bar_left_btn);
        ImageButton navi_right = (ImageButton) findViewById(R.id.title_bar_right_btn);
        TextView navi_title = (TextView) findViewById(R.id.title_bar_title);
        findViewById(R.id.title_bar_logo_title).setVisibility(View.GONE);
        findViewById(R.id.title_bar_right_text).setVisibility(View.GONE);
        if (resLeft == 0) {
            navi_left.setVisibility(View.GONE);
        } else {
            navi_left.setVisibility(View.VISIBLE);
            navi_left.setImageResource(resLeft);
        }
        if (resRight == 0) {
            navi_right.setVisibility(View.GONE);
        } else {
            navi_right.setVisibility(View.VISIBLE);
            navi_right.setImageResource(resRight);
        }
        if (TextUtils.isEmpty(title)) {
            navi_title.setVisibility(View.VISIBLE);
        } else {
            navi_title.setVisibility(View.VISIBLE);
            navi_title.setText(title);
        }
        if (leftAction == null) {
            navi_left.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    onBackPressed();
                }
            });
        } else {
            navi_left.setOnClickListener(leftAction);
        }
        navi_right.setOnClickListener(rightAction);
    }

    public void setTopLogoNavi(String resRight, String title) {
        title = "";
        ImageButton navi_left = (ImageButton) findViewById(R.id.title_bar_left_btn);
        TextView navi_right = (TextView) findViewById(R.id.title_bar_right_text);
        TextView navi_title = (TextView) findViewById(R.id.title_bar_logo_title);
        findViewById(R.id.title_bar_title).setVisibility(View.GONE);
        navi_left.setVisibility(View.VISIBLE);
        // navi_left.setImageResource(R.drawable.home_bar_logo);

        if (TextUtils.isEmpty(resRight)) {
            navi_right.setVisibility(View.GONE);
        } else {
            navi_right.setVisibility(View.VISIBLE);
            navi_right.setText(resRight);
        }
        if (TextUtils.isEmpty(title)) {
            navi_title.setVisibility(View.GONE);
        } else {
            navi_title.setVisibility(View.VISIBLE);
            navi_title.setText(title);
        }
        navi_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
            }
        });

    }

    public void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onStop() {
        super.onStop();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void initPrimaryFragment() {

    }

    @Override
    protected Class<? extends Fragment> getPrimaryFragmentClass(int fragmentId) {

        return null;
    }

    @Override
    protected Bundle getPrimaryFragmentArguments(int fragmentId) {

        return null;
    }

    @Override
    protected int getPrimaryFragmentStubId(int fragmentId) {

        return 0;
    }

    Handler mHandler = new Handler();
    public boolean post(Runnable run) {
        return mHandler.post(run);
    }

    @Override
    protected void onDestroy() {
        try {
            ServiceUtils.unregisterReceiver(this, mNotifyDataReceiver);
            ServiceUtils.unbindService(this, conn);
        }catch (Exception e){
            e.printStackTrace();
        }
        super.onDestroy();
    }
}
